<?php
include '../config/koneksi.php';
include '../class/user.php';

if (!isset($_SESSION['Id'])){
	$togo="../formlogin.php";
	header ("Location:$togo");
	}
if ($_POST){
  if ($_POST['pass_lama']!="" && $_POST['pass_baru']!="" && $_POST['pass_ulangi']!=""){
		$User_Dao = new User_Dao();
		$user=new User();
		$cek = $User_Dao->get ($_SESSION['Id']);
		if($cek->password==$_POST['pass_lama']){
			$user->password=$_POST['pass_baru']; 
			$user->id=$_SESSION['Id'];
			$User_Dao->edit($user);
			echo "<script>alert('Data berhasil disimpan');</script>";
		}
	} else {
		echo "Isikan data yang masih kosong terlebih dahulu!";
	}	
}
?>
<script>
	function validate_form(){
		var pasbar = $("#pass_baru").val();
		var pasbar2 = $("#pass_ulangi").val();
		var paslam = $("#pass_lama").val();
		if(pasbar=="" || pasbar2=="" || paslam==""){
			alert('Lengkapi form Anda!');
			return false;
		}else{
			if(pasbar==pasbar2){
				document.ubahpassword.submit();
			}else{
				alert('Password baru dan konfirmasi password baru tidak sama !');
				return false;
			}
		}
	}
</script>
<table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
  <tr> 
      
    <td height="30""> 
      <div align="left"><font size="3" face="tahoma">Ubah Password</font><hr color=#CCCCFF></div></td>
    </tr>
  </table>
<form id="form1" name="form1" method="post" action="" name="ubahpassword">
  <table width="65%" border="0" align="center" bordercolor="#FF9900" bgcolor="#FFFFFF">
    <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Masukkan Password Lama</font></td>
      <td valign=top>:</td>
      <td valign=top> <input name="pass_lama" type="password" id="pass_lama"></td>
    </tr>
    <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Masukkan Password Baru</font></td>
      <td valign=top>:</td>
      <td valign=top> <input name="pass_baru" type="password" id="pass_baru"></td>
    </tr>
    <tr bordercolor="#FFFFFF"> 
      <td><font size="2" face="tahoma">Masukkan Lagi Password Baru</font></td>
      <td valign=top>:</td>
      <td valign=top> <input name="pass_ulangi" type="password" id="pass_ulangi"></td>
    </tr>
    <tr bordercolor="#FFFFFF"> 
      <td height="21" colspan="2"></td><td><font size="2" face="tahoma"> 
          <input type="submit" name="Simpan" id="Simpan" value="Simpan" onclick="return validate_form();" />
          </font>
        </td>
  </table>
</form>